// optimized CRC16
//uses 2-bytes of global variable
#include <system.h>
char crc_lo, crc_hi;
void UpdateCrc( char data )
{
	char i, lo;
	data ^= crc_hi;
	lo = crc_lo;
	crc_lo = 0;
	crc_hi = 0;
	i=8;
	asm
	{
loop:
		addlw	0x00			//clear carry bit
		rlf		_data, W		//rotate W (modulo 8 instead of 9)
		btfsc	_status, C
		iorlw	0x01
		addlw	0x00			//clear carry bit
		rlf		_crc_lo, F
		rlf		_crc_hi, F
		movwf	_data
		xorwf	_status, W
		andlw	0x01			//clear everything but bit 0
		btfsc	_status, Z
		goto	skip
		movlw	0x10
		xorwf	_crc_hi, F
		movlw	0x21
		xorwf	_crc_lo, F
skip:
		decfsz	_i, F
		goto loop
	}
	crc_hi ^= lo;
}

void ClearCrc()
{
	crc_lo=0;
	crc_hi=0;
}